import 'package:bitcoin_mine/model/check_in.dart';
import 'package:bitcoin_mine/model/contract.dart';
import 'package:bitcoin_mine/model/mine_info.dart';
import 'package:bitcoin_mine/model/pay_order_success.dart';
import 'package:bitcoin_mine/model/register_model.dart';
import 'package:bitcoin_mine/model/withdraw_info.dart';

import '../../generated/json/base/json_convert_content.dart';

class BaseEntity<T> {
  BaseEntity(this.status, this.code, this.data);

  BaseEntity.fromJson(Map<String, dynamic> json) {
    status = json["status"] as int?;
    code = json["code"] as int?;
    if (json.containsKey("data")) {
      if (json["data"] is Map && (json["data"] as Map).isEmpty) {
        data = null;
      } else {
        data = _generateOBJ<T>(json["data"] as Object?);
      }
    }
  }

  int? status;
  int? code;
  T? data;

  T? _generateOBJ<O>(Object? json) {
    if (json == null) {
      return null;
    }
    if (T.toString() == 'String') {
      return json.toString() as T;
    } else if (T.toString() == 'Map<dynamic, dynamic>') {
      return json as T;
    } else if (T.toString() == 'List<String>') {
      return (json as List).map((e) => e as String).toList() as T;
    } else if (T.toString() == 'RegisterModel') {
      return RegisterModel.fromJson(json as Map<String, dynamic>) as T;
    } else if (T.toString() == 'MineInfo') {
      return MineInfo.fromJson(json as Map<String, dynamic>) as T;
    } else if (T.toString() == 'CheckIn') {
      return CheckIn.fromJson(json as Map<String, dynamic>) as T;
    } else if (T.toString() == 'CheckInDetail') {
      return CheckInDetail.fromJson(json as Map<String, dynamic>) as T;
    } else if (T.toString() == 'Contract') {
      return Contract.fromJson(json as Map<String, dynamic>) as T;
    } else if (T.toString() == 'List<Contract>') {
      return (json as List).map((e) => Contract.fromJson(e)).toList() as T;
    } else if (T.toString() == 'List<WithdrawInfo>') {
      return (json as List).map((e) => WithdrawInfo.fromJson(e)).toList() as T;
    } else if (T.toString() == 'PayOrderSuccess') {
      return PayOrderSuccess.fromJson(json as Map<String, dynamic>) as T;
    } else {
      /// List类型数据由fromJsonAsT判断处理
      return JsonConvert.fromJsonAsT<T>(json);
    }
  }
}
